<script>
	var url;
	$(document).ready(function(){
		
		// kamus data
		
		saveData = function(){
			endEditing();
			rows = $('#dg-matakuliah').datagrid('getData');
			//alert(JSON.stringify(rows));
			//return 0;
			$.ajax({
			  url: base_url+"konversi_nilai/save/",
			  method: 'POST',
			  data: {
						kd_fakultas : $('#kd_fakultas').val(),
						kd_prodi : $('#kd_prodi').val(),
						kd_kurikulum : $('#kd_kurikulum').val(),
						//id_tahunajaran : $('#id_tahunajaran').val(),
						nim : $('#nim').combogrid('getValue'),
						//semester : $('#semester').val(),
						//sks : $('#sks').val(),
						kd_dosen : $('#kd_dosen').val(),
						data : rows
					},
			  success : function(response, textStatus){
				//alert(response);
				var response = eval('('+response+')');
				if(response.success){
					$.messager.show({
						title: 'Success',
						msg: 'Data Berhasil Disimpan'
					});
					$('#dialog').dialog('close');
					$('#dg').datagrid('reload');
				}else{
					$.messager.show({
						title: 'Error',
						msg: response.msg
					});
				}
			  }
			});
		}
		//end saveData
		
		hitung_sks = function(){
			try{
				var total = 0;
				// get row selected
				rows = $('#dg-matakuliah').datagrid('getData');
				for(var i=0; i<rows.total; i++){
					if(rows.rows[i].chk == 1){
						total += parseInt(rows.rows[i].sks);
					}
				}
				$('#sks').val(total);
			}catch(e){}
		}
		
		// load mahasiswa
		load_mahasiswa = function(){
			$('#nim').combogrid({
				panelWidth:450,
				panelHeight:275,
				value:'',
			 
				idField:'nim',
				textField:'nama',
				url:base_url+'prosedur/getMutasiMahasiswa/'+$('#kd_prodi').val(),
				columns:[[
					{field:'nim',title:'NIM',width:130},
					{field:'nama',title:'Nama',width:250}
				]],
				onChange: function(value){
					getsemester();
				}
			});
		}
		
		load_dosen = function(){
			$('#kd_dosen').load(base_url+'prosedur/getDosenbyProdi/'+$('#kd_prodi').val());
		}
		
		getsemester = function(){
			$.getJSON(base_url+'krs/getsemester_mhs/'+$('#nim').combogrid('getValue'), function( data ) {
				$('#semester').val(data.semester);
			});
		}
		
		// onchange
		$('#kd_fakultas').change(function(){
			$('#kd_prodi').load(base_url+'prosedur/getProdibyFakultas/'+$('#kd_fakultas').val());
			//load_matakuliah();
		});
		$('#kd_prodi').change(function(){
			$('#kd_kurikulum').load(base_url+'prosedur/getKurikulumbyProdi/'+$('#kd_prodi').val());
			//load_matakuliah();
			load_mahasiswa();
			load_dosen();
		});
		$('#kd_kurikulum').change(function(){
			//load_matakuliah();
		});
		
		$('#id_tahunajaran').change(function(){
			var val = $('#id_tahunajaran').val();
			if(val.substring() == '3'){
				$('#semester').val('SP');
			}else{
				getsemester();
			}
			load_matakuliah();
		});
		
		
		// editing cell
		$.extend($.fn.datagrid.methods, {
			editCell: function(jq,param){
				return jq.each(function(){
					var opts = $(this).datagrid('options');
					var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
					for(var i=0; i<fields.length; i++){
						var col = $(this).datagrid('getColumnOption', fields[i]);
						col.editor1 = col.editor;
						if (fields[i] != param.field){
							col.editor = null;
						}
					}
					$(this).datagrid('beginEdit', param.index);
					for(var i=0; i<fields.length; i++){
						var col = $(this).datagrid('getColumnOption', fields[i]);
						col.editor = col.editor1;
					}
				});
			}
		});
		
		var editIndex = undefined;
		endEditing = function(){
			if (editIndex == undefined){return true}
			if ($('#dg-matakuliah').datagrid('validateRow', editIndex)){
				
				// kimochi. set colum nama dosen, sebelum selesai edit
				var cmb = $('#dg-matakuliah').datagrid('getEditor',{
					index:editIndex,
					field:'kd_bobot'
				});
				if(cmb){
					$('#dg-matakuliah').datagrid('updateRow',{
						index: editIndex,
						row: {
							kd_bobot: $(cmb.target).combobox('getValue'),
							kd_bobot: $(cmb.target).combobox('getText')
						}
					});
				}
			    
				// close editor and save
				$('#dg-matakuliah').datagrid('endEdit', editIndex);
				editIndex = undefined;
				//hitung_sks();
				return true;
			} else {
				return false;
			}
		}
		
		onClickCells = function(index, field, row){
			// kimochi, get kode matkul
			var dat = $('#dg-matakuliah').datagrid('getData');
			var kd_matkul = dat.rows[index].kd_matakuliah;
			
			// kimochi
			// change column option
			var opts = $('#dg-matakuliah').datagrid('getColumnOption', 'kd_bobot');
			opts.editor = {
				type:'combobox',
				options:{
					mode:'remote',
					valueField:'kd_bobot',
					textField:'kd_bobot',
					editable:false,
					url:base_url+'konversi_nilai/load_kd_bobot',
					/*onChange:function(value){
						 var cc = this;
						 //alert($(cc).combobox('getText'));
						 if($(cc).combobox('getText') != ''){
							$('#dg-matakuliah').datagrid('updateRow',{
								index: index,
								row: {
									id_jadwal: $(cc).combobox('getValue'),
									nama_dosen: $(cc).combobox('getText')
								}
							});
						 }
						 //setTimeout(function(){
							 //var rowx = $('#dg-matakuliah').datagrid('getRows')[opts.editIndex];
							 //rowx.nama_dosen = $(cc).combobox('getText');
							// $('#dg-matakuliah').datagrid('updateRow',{
								// index: index,
								// row: {
									// id_jadwal: $(cc).combobox('getValue'),
									// nama_dosen: $(cc).combobox('getText')
								// }
							// });
						 //},0);
					 }*/
				}
			};
			if (endEditing()){
				$('#dg-matakuliah').datagrid('selectRow', index)
						.datagrid('editCell', {index:index,field:field});
				editIndex = index;
			}
		}
		
		FormatterCell_id_jadwal = function(value,row,index){
			return row.kd_bobot;
		}
		
		update_value = function(index, value, kd_bobot){
			//alert(id_jadwal);
			$('#dg-matakuliah').datagrid('updateRow',{index:index, row:{chk:(value==true?1:0)}});
			//hitung_sks();
			//alert(id_jadwal);
			// if(id_jadwal != null || id_jadwal != '0' || id_jadwal != '' || id_jadwal != ' ' || id_jadwal != '&nbsp;'){
				
			// }else{
				
			// }
		}
		
		Checkbox = function(value, row, index){
			return '<input style="margin-top:2px;" type="checkbox" name="checkbox" id="checkbox" onclick="update_value('+index+', this.checked, \''+row.kd_bobot+'\')" '+(row.chk==true?'checked="checked"':'')+'/>';
		}
		
		// load matkul
		$(function(){ // init
			$('#dg-matakuliah').datagrid({
				url:"<?=base_url()?>konversi_nilai/getdata_matkul"
			});
				//$('#dg').datagrid('enableFilter'); 
		});	
		
		
		load_matakuliah = function(){
			$('#dg-matakuliah').datagrid('load',{
				kd_fakultas : $('#kd_fakultas').val(),
				kd_prodi : $('#kd_prodi').val(),
				kd_kurikulum : $('#kd_kurikulum').val(),
				//id_tahunajaran : $('#id_tahunajaran').val()
			});
		}
		
		
		
		// $('#dg-matakuliah').datagrid({
			// onCheck: function(index, row){
						// hitung_sks();
					// },
			// onUncheck: function(index, row){
						// hitung_sks();
					// },
			// onCheckAll: function(index, row){
						// hitung_sks();
					// },
			// onUncheckAll: function(index, row){
						// hitung_sks();
					// }
		// });
	});
</script>

<table id="dg-matakuliah" title="Konversi Nilai" data-options="
			rownumbers:true,
			singleSelect:false,
			autoRowHeight:false,
			pagination:false,
			pageSize:30,
			fit:true,
			toolbar:'#toolbar-matakuliah',
			view:groupview,
			groupField:'semester',
			groupFormatter:function(value, rows){
				return 'Semester ' + value;
			},
			onClickCell: onClickCells,
			rowStyler: function(index,row){
				if (row.chk == 1){
					return 'background-color:#feffc1;color:#000;';
				}
			}
		">		
	<thead>
		<tr>
			<!--<th field="tahun_penerimaan" sortable="true" width="80">Tahun Penerimaan</th> -->
			<!--<th data-options="field:'ck',checkbox:true"></th>-->
			<th field="kd_matakuliah" width="100">Kode Matkul</th>
			<th field="nama_matakuliah" width="300">Nama Matkul</th>
			<th field="sks" width="75">SKS</th>
			<th field="semester" width="75">Semester</th>
			<!--th field="kd_jenismk" width="75">Jenis Matkul</th-->
			<th field="kd_bobot" width="200" formatter="FormatterCell_id_jadwal">Kelas/Dosen</th>
			<!--th field="kd_bobot" width="200" hidden="true">ID Jadwal</th>
			<th field="chk" width="23" formatter="Checkbox"></th-->
			<th field="chk" width="23" formatter="Checkbox"></th>
		</tr>
	</thead>
</table>
<div id="toolbar-matakuliah" style="padding:5px;height:auto">
	<div>
		<table width="700" border="0">
		  <tr>
			<td>Fakultas</td>
			<td>: 
				<select id="kd_fakultas" name="kd_fakultas" style="width:220px;">
					<?=$this->mdl_prosedur->OptionFakultas();?>
				</select>
			</td>
			<td>&nbsp;</td>
			<td>Tahun Ajaran</td>
			<td>: 
				<select id="id_tahunajaran" name="id_tahunajaran" style="width:220px;">
					<?=$this->mdl_prosedur->OptionTahunAjaran();?>
				</select>
			</td>
		  </tr>
		  <tr>
			<td>Program Studi</td>
			<td>: 
				<select id="kd_prodi" name="kd_prodi" style="width:220px;">
					<?=$this->mdl_prosedur->OptionProdi();?>
				</select>
			</td>
			<td>&nbsp;</td>
			<td>Mahasiswa</td>
			<td>: 
				<input type="textfield" id="nim" name="nim" size="33" value="">
			</td>
		  </tr>
		  <tr>
			<td>Kurikulum</td>
			<td>: 
				<select id="kd_kurikulum" name="kd_kurikulum" style="width:220px;">
					<?=$this->mdl_prosedur->OptionKurikulum();?>
				</select>
			</td>
			<td>&nbsp;</td>
			<td>Pembimbing</td>
			<td>: 
				<select id="kd_dosen" name="kd_dosen" style="width:220px;">
					
				</select>
			</td>
		  </tr>
		  <tr>
			<td></td>
			<td></td>
			<td>&nbsp;</td>
			
		  </tr>
		</table>
	</div>
	<div align="left"><a href="#" class="easyui-linkbutton" onclick="saveData();" iconCls="icon-save" plain="false">Simpan</a>&nbsp;&nbsp;&nbsp;</div>
</div>



